package com.cxh.rmsq.ui.activity.self

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.TimeUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityOrderDetailBinding
import com.cxh.rmsq.databinding.ActivityOrderListBinding
import com.cxh.rmsq.net.newNet.TreasureViewModel
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.utils.ToastUtils
import com.cxh.rmsq.utils.loadImage

class OrderDetailActivity : TitleBaseActivity() {
    private lateinit var binding: ActivityOrderDetailBinding
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(TreasureViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityOrderDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        titleBar.tvTitle.text = "订单详情"
        var orderId = intent.getStringExtra("id").toString()
        viewModel.getOrderDetail(orderId) {
            if (it.data != null && it.code == 200) {
                binding.tvOrderNo.text = it.data.orderNo
                binding.tvGoodsName.text = it.data.productName

                binding.tvPrice.text = "¥" + it.data.totalPrice
                binding.tvAllPrice.text = "¥" + it.data.payPrice
                binding.tvOrderTime.text = TimeUtils.millis2String(it.data.createTime!!)

                loadImage(it.data.imgUrl, binding.ivImg)

                if (it.data.payStatus == 0) {//支付状态 0：待支付 1：已支付 2：已取消 3 失效
                    binding.llPayTime.isVisible = false
                    binding.llClue.isVisible = false
                    binding.tvTime.isVisible = true
                    binding.llPrice.isVisible = true
                    binding.view.isVisible = true
                    binding.tvTime.text = "请在：" + TimeUtils.getFitTimeSpan(  //getTimeSpan
                        it.data.expireTime!!,
                        System.currentTimeMillis(),
                        4
                    ) + "完成支付"
                    binding.tvStatus.text = "待支付"
                    binding.ivStatus.setImageResource(R.mipmap.icon_order_daifukuan)
                } else if (it.data.payStatus == 1) {
                    binding.llPayTime.isVisible = true
                    binding.llClue.isVisible = true
                    binding.tvTime.isVisible = false
                    binding.llPrice.isVisible = false
                    binding.view.isVisible = false
                    binding.ivStatus.setImageResource(R.mipmap.icon_order_paied)

                    binding.tvStatus.text = "已支付"
                    binding.tvLocaiton.text = it.data.remark
                    binding.tvPayTime.text = TimeUtils.millis2String(it.data.payTime!!)

                } else if (it.data.payStatus == 2) {
                    binding.llClue.isVisible = false
                    binding.llPayTime.isVisible = false
                    binding.tvTime.isVisible = false
                    binding.llPrice.isVisible = false
                    binding.view.isVisible = false
                    binding.tvStatus.text = "已取消"
                    binding.ivStatus.setImageResource(R.mipmap.icon_order_paied)
                } else {
                    binding.llClue.isVisible = false
                    binding.llPayTime.isVisible = false
                    binding.tvTime.isVisible = false
                    binding.llPrice.isVisible = false
                    binding.view.isVisible = false
                    binding.tvStatus.text = "已失效"
                    binding.ivStatus.setImageResource(R.mipmap.icon_order_paied)
                }
            } else {
                ToastUtils.showToast(it.msg)
                finish()
            }
        }

        binding.tvDaohang.setOnClickListener { //导航

        }
        binding.stvCanclePay.setOnClickListener { //取消支付

        }
        binding.stvPay.setOnClickListener { //支付

        }
    }
}